//
//  JMRelegaDemotionVController.m
//  jiMuOperate
//
//  Created by 胡梅方 on 16/5/4.
//  Copyright © 2016年 jinXinFengLi. All rights reserved.
//

#import "JMRelegaDemotionVController.h"
#import "BuyPineView.h"
#import "JMGreenDegraView.h"
#import "JMRedDegraView.h"

@interface JMRelegaDemotionVController ()<UITextFieldDelegate>
@property(nonatomic,weak)UIButton *confirm;
@property(nonatomic,weak)BuyPineView *buyPineView;
@property(nonatomic,weak)JMGreenDegraView *greenView;
@property(nonatomic,weak)JMRedDegraView *redView;
@property(nonatomic,weak)UserEntity *user;
@end

@implementation JMRelegaDemotionVController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"松果保级－松籽充值";
     self.view.backgroundColor = BACKCOLOR;
//    self.view.backgroundColor = [UIColor whiteColor];
    self.user = [UserEntity shardUserInfo];
    NSArray *array=@[@"买松籽",@"绿果降级",@"红果降级"];
    UISegmentedControl *segmentedControl=[[UISegmentedControl alloc]initWithItems:array];
    segmentedControl.frame=CGRectMake(10, 74,300, 40);
    segmentedControl.frame = [ToolViewAndData MyAutoLayout:segmentedControl];
    segmentedControl.selectedSegmentIndex=0;
    segmentedControl.tintColor=[UIColor redColor];

    [self.view addSubview:segmentedControl];
       segmentedControl.tintColor = RGB(211, 0, 23);
//     下面的代码实同正常状态和按下状态的属性控制,比如字体的大小和颜色等
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12],NSFontAttributeName,[UIColor redColor],NSForegroundColorAttributeName,nil, nil];
    
    [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
    
    
    NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
    
    [segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
    
    //设置分段控件点击相应事件
    [segmentedControl addTarget:self action:@selector(doSomethingInSegment:)forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:segmentedControl];
    
    
    UIButton *confirm = [ARC createButtonWithFrame:CGRectMake(20, 410, 280, 40) Target:self Sel:@selector(confirmBtnClick:)];
    self.confirm = confirm;
    confirm.frame = [ToolViewAndData MyAutoLayout:confirm];
    [confirm setBackgroundImage:[UIImage imageNamed:@"logoin_btn"] forState:UIControlStateNormal];
    [confirm setTitle:@"确认支付" forState:UIControlStateNormal];
    [self.view addSubview:confirm];
    

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    HIDDENNAV(NO);
    CGFloat viewX = 0;
    CGFloat viewY = 120;
    CGFloat viewW = 320;
    BuyPineView *buyView = [[BuyPineView alloc]initWithFrame:CGRectMake(viewX, viewY, viewW, 200)];
    buyView.frame = [ToolViewAndData MyAutoLayout:buyView];
    self.buyPineView = buyView;
    buyView.purchaseNum.delegate = self;
    buyView.currentNum.text =self.user.PineNut;
    
    [self.view addSubview:buyView];
    buyView.backgroundColor = [UIColor whiteColor];
    
    JMGreenDegraView *greenView = [[JMGreenDegraView alloc]initWithFrame:CGRectMake(viewX, viewY, viewW, 280)];
    greenView.frame = [ToolViewAndData MyAutoLayout:greenView];
    self.greenView = greenView;
    greenView.paymentPwdNum.delegate = self;
    [self.view addSubview:greenView];
    greenView.backgroundColor = [UIColor whiteColor];
    
    
    JMRedDegraView *redView = [[JMRedDegraView alloc]initWithFrame:CGRectMake(viewX, viewY, viewW, 285)];
    redView.frame = [ToolViewAndData MyAutoLayout:redView];
    self.redView = redView;
    redView.paymentPwdNum.delegate = self;
    [self.view addSubview:redView];
    redView.backgroundColor = [UIColor whiteColor];
    
    self.buyPineView.hidden = NO;
    self.greenView.hidden = YES;
    self.redView.hidden = YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    self.buyPineView.afterPurNum.text = [NSString stringWithFormat:@"%d",[self.user.PineNut integerValue]+[self.buyPineView.purchaseNum.text integerValue]];
    self.buyPineView.totalNum.text = [NSString stringWithFormat:@"￥%@",self.buyPineView.purchaseNum.text];
    [textField resignFirstResponder];
    return YES;
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.buyPineView.afterPurNum.text = [NSString stringWithFormat:@"%d",[self.user.PineNut integerValue]+[self.buyPineView.purchaseNum.text integerValue]];
    self.buyPineView.totalNum.text = [NSString stringWithFormat:@"￥%@",self.buyPineView.purchaseNum.text];
    [self.buyPineView.purchaseNum resignFirstResponder];
    [self.greenView.paymentPwdNum resignFirstResponder];
    [self.redView.paymentPwdNum resignFirstResponder];
}
-(void)confirmBtnClick:(UIButton*)btn
{
    
}

-(void)doSomethingInSegment:(UISegmentedControl *)Seg
{
    
    NSInteger Index = Seg.selectedSegmentIndex;
    
    switch (Index)
    {
        case 0:{
            self.buyPineView.hidden = NO;
            self.redView.hidden = YES;
            self.greenView.hidden = YES;
              self.navigationItem.title = @"松果保级－松籽充值";
            [self.confirm setTitle:@"确认支付" forState:UIControlStateNormal];

            break;
        }
        case 1:
            self.buyPineView.hidden = YES;
            self.redView.hidden = YES;
            self.greenView.hidden = NO;
              self.navigationItem.title = @"松果保级－绿果降级";
            [self.confirm setTitle:@"确认降级" forState:UIControlStateNormal];

            break;
        case 2:
            self.buyPineView.hidden = YES;
            self.redView.hidden = NO;
            self.greenView.hidden = YES;
              self.navigationItem.title = @"松果保级－红果降级";
            [self.confirm setTitle:@"确认降级" forState:UIControlStateNormal];

            break;
        default:
            break;
    }
}
@end
